package com.ruoyi.applet.appletShoppingCart.service.impl;

import com.ruoyi.applet.appletShoppingCart.domain.AppletShoppingCart;
import com.ruoyi.applet.appletShoppingCart.mapper.AppletShoppingCartMapper;
import com.ruoyi.applet.appletShoppingCart.service.IAppletShoppingCartService;
import com.ruoyi.applet.entity.PageR;
import com.ruoyi.applet.entity.ShoppingCartApplet;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 购物车Service业务层处理
 * @author ruoyi
 * @date 2024-11-28
 */
@Service
public class AppletShoppingCartServiceImpl implements IAppletShoppingCartService {
    
    @Resource
    private AppletShoppingCartMapper appletShoppingCartMapper;
    
    /**
     * 查询购物车
     * @param id 购物车主键
     * @return 购物车
     */
    @Override
    public AppletShoppingCart selectAppletShoppingCartById(Long id) {
        return appletShoppingCartMapper.selectAppletShoppingCartById(id);
    }
    
    /**
     * 查询购物车列表
     * @param appletShoppingCart 购物车
     * @return 购物车
     */
    @Override
    public List<AppletShoppingCart> selectAppletShoppingCartList(AppletShoppingCart appletShoppingCart) {
        return appletShoppingCartMapper.selectAppletShoppingCartList(appletShoppingCart);
    }
    
    /**
     * 新增购物车
     * @param appletShoppingCart 购物车
     * @return 结果
     */
    @Override
    public int insertAppletShoppingCart(AppletShoppingCart appletShoppingCart) {
        appletShoppingCart.setCreateTime(DateUtils.getNowDate());
        return appletShoppingCartMapper.insertAppletShoppingCart(appletShoppingCart);
    }
    
    /**
     * 修改购物车
     * @param appletShoppingCart 购物车
     * @return 结果
     */
    @Override
    public int updateAppletShoppingCart(AppletShoppingCart appletShoppingCart) {
        appletShoppingCart.setUpdateTime(DateUtils.getNowDate());
        return appletShoppingCartMapper.updateAppletShoppingCart(appletShoppingCart);
    }
    
    /**
     * 批量删除购物车
     * @param ids 需要删除的购物车主键
     * @return 结果
     */
    @Override
    public int deleteAppletShoppingCartByIds(String ids) {
        return appletShoppingCartMapper.deleteAppletShoppingCartByIds(Convert.toStrArray(ids));
    }
    
    /**
     * 删除购物车信息
     * @param id 购物车主键
     * @return 结果
     */
    @Override
    public int deleteAppletShoppingCartById(Long id) {
        return appletShoppingCartMapper.deleteAppletShoppingCartById(id);
    }
    
    /**
     * 获取购物车列表
     * @param shoppingCartApplet 条件
     * @return {@link PageR < ShoppingCartApplet >}
     */
    @Override
    public PageR<AppletShoppingCart> selectShoppingCartListApplet(ShoppingCartApplet shoppingCartApplet) {
        int count = appletShoppingCartMapper.selectShoppingCartListAppletCount(shoppingCartApplet);
        PageR<AppletShoppingCart> pageR = new PageR<>();
        pageR.setPageNo(shoppingCartApplet.getPageNo());
        pageR.setPageSize(shoppingCartApplet.getPageSize());
        pageR.setTotal(count);
        if (count == 0) {
            return pageR;
        }
        int start = (shoppingCartApplet.getPageNo() - 1) * shoppingCartApplet.getPageSize();
        List<AppletShoppingCart> shoppingCartAppletList = appletShoppingCartMapper.selectShoppingCartListApplet(shoppingCartApplet, shoppingCartApplet.getPageSize(), start);
        
        pageR.setReCords(shoppingCartAppletList);
        return pageR;
    }
    
    /**
     * 添加到购物车
     * @param shoppingCart 条件
     * @return {@link int}
     */
    @Override
    public int insertAppletShoppingCartApplet(AppletShoppingCart shoppingCart) {
        shoppingCart.setAddTime(DateUtils.getNowDate());
        shoppingCart.setDelFlag(1);
        shoppingCart.setCreateTime(DateUtils.getNowDate());
        shoppingCart.setCreateBy(shoppingCart.getWxUserId().toString());
        return appletShoppingCartMapper.insertAppletShoppingCart(shoppingCart);
    }
    
    /**
     * 获取购物车
     * @param appletShoppingCart 条件
     * @return {@link AppletShoppingCart}
     */
    @Override
    public AppletShoppingCart selectAppletShoppingCartListBySole(AppletShoppingCart appletShoppingCart) {
        return appletShoppingCartMapper.selectAppletShoppingCartListBySole(appletShoppingCart);
    }
}
